package com.example.communist_app_api.config.redis;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/2/20 14:54
 */
@Configuration
public class RedisConfig {
    public RedisConfig() {
    }
//
//    @Bean
//    @ConditionalOnMissingBean(
//            name = {"redisTemplate"}
//    )
//    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
//    @SuppressWarnings("all")
//    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
//        RedisTemplate<String, Object> template = new RedisTemplate();
//        template.setConnectionFactory(redisConnectionFactory);
//        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
//        ObjectMapper objectMapper = new ObjectMapper();
////		objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//        objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator());
//        jackson2JsonRedisSerializer.setObjectMapper(objectMapper);
//        //String 的序列化
//        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
//        //key采用String的序列化
//        template.setKeySerializer(stringRedisSerializer);
//        //hash的key采用String的序列化
//        template.setHashKeySerializer(stringRedisSerializer);
//        //value序列化方式采用json
//        template.setValueSerializer(jackson2JsonRedisSerializer);
//        //hash的value序列化方式采用json
//        template.setHashValueSerializer(jackson2JsonRedisSerializer);
//        template.afterPropertiesSet();
//
//        return template;
//    }

    @Resource
    private RedisTemplate redisTemplate;

    @Bean
    public RedisTemplate redisTemplateInit(){
        //序列化key的实例化对象
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //序列化value的实例化对象
        redisTemplate.setValueSerializer(
                new GenericJackson2JsonRedisSerializer());
        return redisTemplate;
    }



    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnSingleCandidate(RedisConnectionFactory.class)
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory) {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
}
